<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">欢迎登录</h3>
      </div>

      <el-form-item prop="username">
        <div style="display: flex;align-items: center;">
          <i class="el-icon-user" style="color: #ffffff;margin-left: 20px"></i>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </div>

      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="大写已经锁定"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <div style="display: flex;align-items: center;">
            <i class="el-icon-lock" style="color: #ffffff;margin-left: 20px"></i>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
            <img src="@/assets/images/eye.svg" alt="" v-if="passwordType === 'password'">
            <img src="@/assets/images/eye-open.svg" alt="" v-else>
          </span>
          </div>
        </el-form-item>
      </el-tooltip>

      <el-form-item prop="password">
        <div style="display: flex;align-items: center;">
          <el-input
            v-model="loginForm.captcha"
            placeholder="请输入验证码"
            @keyup.enter.native="handleLogin"
          />
          <span style="cursor: pointer;display: inline-block;height: 50px;display: flex;align-items: center;justify-content: center" @click="handleChangeCheckCode">
              <img :src="codeUrl" alt="" v-if="codeUrl" >
              <img v-else src="@/assets/images/checkcode.png"/>
          </span>
        </div>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录
      </el-button>
    </el-form>
  </div>

</template>
<script>
import {mapActions} from "vuex"
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
        captcha:''
      },
      codeUrl:'',
      loginRules: {
        //来自校验规则文件 utils/validate.form.js
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
    };
  },
  async asyncData({$axios}){
    try {
      let res = await $axios.get('http://localhost:8080/jeecg-boot/sys/randomImage/1629428467008');
      return {
        codeUrl: res.data.result
      };
    } catch (error) {
      console.error('Request failed:', error);
      return {
        data: ''
      };
    }
  },
  created() {
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
  },
  methods: {
    ...mapActions({
      setToken:'user/setToken',
      setUserInfo:'user/setUserInfo'
    }),

    async handleChangeCheckCode(){
      let res = await this.$axios.get('http://localhost:8080/jeecg-boot/sys/randomImage/1629428467008')
      this.codeUrl=res.data.result||'';
    },
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (
          (shiftKey && key >= "a" && key <= "z") ||
          (!shiftKey && key >= "A" && key <= "Z")
        ) {
          this.capsTooltip = true;
        } else {
          this.capsTooltip = false;
        }
      }
      if (key === "CapsLock" && this.capsTooltip === true) {
        this.capsTooltip = false;
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          let params={
            ...this.loginForm,
            checkKey:1629428467008
          }
          this.$axios({
            url:'/api/sys/login',
            method:'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            data: params
          }).then(res => {
            if(res.data.success){
              this.setToken(res.data.result.token)
              this.setUserInfo(res.data.result.userInfo)
              this.$router.push('/')
            }else{
              this.$message.error(res.data.message);
              this.loginForm.captcha=''
              this.handleChangeCheckCode()
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};

</script>
<style>
/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    color: #fff;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #283443 inset !important;
        -webkit-text-fill-color: #fff!important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style scoped lang="less">
.login-container {
  min-height: 100vh;
  overflow: hidden;
  width: 100%;
  // 增加背景图片
  overflow: hidden;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  height: 100%;
  background-image: url("@/assets/images/share.jpg");
  background-size: cover;

  .login-form {
    position: relative;
    width: 320px;
    max-width: 100%;

    padding: 65px 35px 20px;
    overflow: hidden;
    background: rgba(75, 68, 68, 0.6);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .title-container {
    position: relative;

    .title {
      font-size: 1.17em;
      color: #fff;
      letter-spacing: 6px; //字符间距
      margin: 0px auto 30px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
    >img{
      width: 20px;
    }
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

::v-deep .el-form-item__content {
  display: flex;
}

::v-deep .el-input__inner {
  color: #ffffff !important;
}
</style>
